package com.winchain.chainbill.bill.biz.repurchase.controller;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.acct.account.util.VirtualAcctUtil;
import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.utils.CommonFileUtil;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbRpurService;
import com.winchain.chainbill.bill.biz.query.service.BillQueryService;
import com.winchain.chainbill.bill.biz.query.vo.CbRecQueryVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import com.winchain.chainbill.bill.biz.repurchase.service.BillRpurPmtService;
import com.winchain.chainbill.bill.biz.repurchase.service.BillRpurQueryService;
import com.winchain.chainbill.bill.biz.repurchase.service.BillRpurService;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import com.winchain.chainbill.risk.utill.RateUtil;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BillRpurController {
	@Autowired
	private BillRpurQueryService billRpurQueryService;
	@Autowired
	private BillRpurService billRpurService;
	@Autowired
	private BillRpurPmtService billRpurPmtService;
	@Autowired
	private BillQueryService billQueryService;
	@Autowired
	private CbRpurService cbRpurService;
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private BillAcctService billAcctService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public List<CbRecVo> selectBillForRpurByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		return this.billRpurQueryService.selectBillForRpurNotInvoiceByPage(limit, cbRecordQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public RetMsg repurchase(CbRpur cbRpur, String fileIds) {
		BatchBillStats<CbRpur> stats = null;
		try {
			stats = this.billRpurService.rpur(cbRpur, fileIds);
		} catch (Exception e) {
			return new RetMsg("error", false, e.getMessage());
		}
		return new RetMsg("success", false, "��������������", stats.getTxSnHost());
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������������")
	public List<CbRecVo> selectBillForRpurPmtByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		return this.billRpurQueryService.selectBillForRpurPmtByPage(limit, cbRecordQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public RetMsg pmt(String billIds) throws Exception {
		if (StringUtil.emptyAndNull(billIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] billIdArr = billIds.split(",");
		BatchBillStats<CbRpur> stats = this.billRpurPmtService.pmt(billIdArr);
		if (billIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "����������������", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "��������������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg rpReject(String remark, String billIds) throws Exception {
		if (StringUtil.emptyAndNull(billIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] billIdArr = billIds.split(",");
		BatchBillStats<CbRpur> stats = this.billRpurPmtService.rpReject(remark, billIdArr);
		if (billIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "��������");
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "��������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> getDetail(String billId, String dctId) {
		CbRecVo bill = this.billQueryService.selectBillDetail(billId);
		CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);

		String rpurFixedRate = RateUtil.getFixedRate(bill.getGrpId(), dct.getFinId(), "rpur_fixed_rate");
		String rpurVariableRate = RateUtil.getVariableRate(bill.getGrpId(), dct.getFinId(), bill.getSenderId(),
				"rpur_variable_rate");
		String rpurEntVariableRate = RateUtil.getEntVariableRate(bill.getGrpId(), bill.getGrpId(),
				CommonDataUtil.getCurEntId(), "rpur_ent_variable_rate");
		String rpurEntVariableRateFin = RateUtil.getEntVariableRate(bill.getGrpId(), dct.getFinId(),
				CommonDataUtil.getCurEntId(), "rpur_ent_variable_rate");
		if (StringUtil.empty(rpurEntVariableRate)) {
			rpurEntVariableRate = rpurEntVariableRateFin;
		}
		String acctType = null;
		if ((ChainBillUtil.onlinePay()) && (VirtualAcctUtil.onVirtual())) {
			acctType = "1";
		} else {
			acctType = "0";
		}
		List<AcctCard> accts = this.billAcctService.getCards(CommonDataUtil.getCurEntId(), acctType);

		AcctBas finAcct = this.billAcctService.getAcctBas(dct.getFinId(), null, "00");
		Map<String, Object> m = new HashMap();
		m.put("rpurFixedRate", rpurFixedRate);
		m.put("rpurVariableRate", rpurVariableRate);
		m.put("rpurEntVariableRate", rpurEntVariableRate);
		m.put("bill", bill);
		m.put("dct", dct);
		m.put("accts", accts);
		m.put("finAcct", finAcct);

		m.put("showSellerAcct", Boolean.valueOf((ChainBillUtil.onlinePay()) && (VirtualAcctUtil.onVirtual())));

		return m;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> getDetailForPaid(String billId) {
		CbRecVo bill = this.billQueryService.selectBillDetail(billId);
		CbRpur rpur = new CbRpur();
		rpur.setBillId(billId);
		rpur.setRpurState("1");
		rpur = (CbRpur) this.cbRpurService.selectOne(rpur);
		CbDct dct = (CbDct) this.cbDctService.selectByKey(rpur.getDctId());

		Map<String, Object> m = new HashMap();
		m.put("bill", bill);
		m.put("dct", dct);
		m.put("rpur", rpur);
		return m;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public boolean checkRate(String billId, String dctId) {
		CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);
		CbRecVo bill = this.billQueryService.selectBillDetail(billId);

		String rpurFixedRate = RateUtil.getFixedRate(bill.getGrpId(), dct.getFinId(), "rpur_fixed_rate");
		if (StringUtil.notEmpty(rpurFixedRate)) {
			return true;
		}
		return false;
	}
}
